/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restaurante.sessionbeans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import restaurante.entidades.Cliente;

/**
 *
 * @author omar
 */
@Stateless
public class ClienteFacade extends AbstractFacade<Cliente> implements ClienteFacadeLocal {
    @PersistenceContext(unitName = "RestauranteEJB-ejbPU")
    private EntityManager em;
    private Query qry;
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ClienteFacade() {
        super(Cliente.class);
    }
    
    @Override
    public List<Cliente> buscaCli(String cadBus){
        qry=em.createQuery("Select c from Cliente c where c.apellidos like '%"+cadBus.trim().toUpperCase()+"%'");
        return (List<Cliente>)qry.getResultList();
    }
    
    
    @Override
    public String obtenerCorrelativo(){
        String var = "CLI_";
        Query qryCount = em.createQuery("Select count(c) from Cliente c");
        if (qryCount.getSingleResult() == null) {
            return var+"1";
        } else {
            return var+(Integer.parseInt(qryCount.getSingleResult().toString()) + 1);
        }
    }
}